#include "monitordisplaywidget.h"

MonitorDisplayWidget::MonitorDisplayWidget(QWidget *parent) : QWidget(parent)
{
    // 获取所有屏幕信息
    screens = QApplication::screens();

    // 设置固定大小以便显示所有显示器
    setFixedSize(400, 300);
}

void MonitorDisplayWidget::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);
    QPainter painter(this);

    // 设置抗锯齿
    painter.setRenderHint(QPainter::Antialiasing);

    // 计算每个矩形的位置和大小
    int rectWidth = width() / 3;
    int rectHeight = height() / 3;
    QRect rects[4];
    rects[0] = QRect(0, 0, rectWidth, rectHeight); // 左上
    rects[1] = QRect(rectWidth, 0, rectWidth, rectHeight); // 右上
    rects[2] = QRect(0, rectHeight, rectWidth, rectHeight); // 左下
    rects[3] = QRect(rectWidth, rectHeight, rectWidth, rectHeight); // 右下

    // 获取当前控件所在的屏幕
    // 使用全局坐标
    QScreen *currentScreen = nullptr;
    if (!screens.isEmpty()) {
        QPoint globalPos = mapToGlobal(QPoint(0, 0)); //获取控件在全局坐标中的位置
        currentScreen = QApplication::screenAt(globalPos);
    }

    // 绘制四个矩形
    for (int i = 0; i < 4 && i < screens.size(); ++i) {
        // 如果是当前屏幕则用绿色，否则用蓝色
        if (screens[i] == currentScreen) {
            painter.setBrush(Qt::green);
        } else {
            painter.setBrush(Qt::blue);
        }

        painter.setPen(Qt::black);
        painter.drawRect(rects[i]);

        // 显示显示器名称
        painter.setPen(Qt::white);
        QString displayName = screens[i]->name();
        if (displayName.isEmpty()) {
            displayName = QString("Display %1").arg(i+1);
        }
        painter.drawText(rects[i], Qt::AlignCenter, displayName);
    }
}
